<template>
  <div class="page-container">
    <div class="control-container">
      <div class="right-panel">
        <div class="search-panel">
          <el-form ref="queryForm" :inline="true" label-width="100" :model="query">
            <el-form-item label="关键字">
              <el-input v-model="query.blurry" placeholder="请输入关键字" maxlength="255" />
            </el-form-item>
            <el-button type="primary" @click="handleAddUser">新增角色</el-button>
            <el-button type="primary" @click="handleQuery">查询</el-button>
            <el-button @click="clearQuery">重置</el-button>
          </el-form>
        </div>
        <div class="table-panel">
          <el-table
            v-loading="tableLoading"
            border
            size="small"
            highlight-current-row
            :data="tableData"
            style="width: 100%"
          >
            <el-table-column prop="name" label="角色名称" show-overflow-tooltip />
            <el-table-column prop="roleType" label="账号类型">
              <template slot-scope="scope">{{ getDictLabel('e036', scope.row.roleType) }}
              </template>
            </el-table-column>
            <el-table-column prop="description" label="备注" show-overflow-tooltip />
            <el-table-column label="操作">
              <template slot-scope="scope">
                <el-button type="primary" size="mini" @click.native.stop="handleUpdate(scope.row)">修改</el-button>
                <el-button type="primary" size="mini" @click.native.stop="handleDel(scope.row)">删除</el-button>
              </template>
            </el-table-column>
            <el-table-column label="分配权限">
              <template slot-scope="scope">
                <el-button type="primary" size="mini" @click="allotJurisdiction(scope.row)">分配权限</el-button>
              </template>
            </el-table-column>
            <el-table-column label="报警类型">
              <template slot-scope="scope">
                <el-button type="primary" size="mini" @click="deployAlarm(scope.row)">配置</el-button>
              </template>
            </el-table-column>
          </el-table>
          <div class="pagination-container">
            <pagination
              v-show="query.total>0"
              :total="query.total"
              :page.sync="query.page"
              :limit.sync="query.limit"
              @pagination="initData"
            />
          </div>
        </div>
      </div>
    </div>
    <AddRoleFormDialog
      :dialog-visible="formDialogVisible"
      :obj="obj"
      :dialog-type="dialogType"
      @flushPage="flushPage"
      @closeDialog="closeDialog"
    />
    <AddCallPoliceDialog
      :obj="obj"
      :dialog-visible="alarmDialogVisible"
      @closeDialog="closeDialog"
      @flushPage="flushPage"
    />
    <AddJurisdictionDialog
      :obj="obj"
      :dialog-visible="jurisdictionDialogVisible"
      @closeDialog="closeDialog"
      @flushPage="flushPage"
    />
  </div>
</template>

<script>
import AddRoleFormDialog from './components/AddRoleFormDialog'
import AddCallPoliceDialog from './components/AddCallPoliceDialog'
import AddJurisdictionDialog from './components/AddJurisdictionDialog'
import Pagination from '@/components/Pagination'
import { getPage, del } from '@/api/system/roles'
import { parseTime, getDictLabel } from '@/utils/index'

export default {
  name: 'SystemRolePage',
  components: {
    AddRoleFormDialog,
    Pagination,
    AddCallPoliceDialog,
    AddJurisdictionDialog
  },
  data() {
    return {
      obj: null,
      dialogType: 'add',
      formDialogVisible: false,
      tableLoading: false,
      query: {
        page: 1, limit: 10, total: 0, deptId: '',
        blurry: ''
      },
      tableData: [],
      alarmDialogVisible: false,
      jurisdictionDialogVisible: false
    }
  },
  created() {
    this.initData()
  },
  methods: {
    parseTime, getDictLabel,
    clearQuery() {
      this.query = {
        page: 1, limit: 10, total: 0,
        deptId: '',
        blurry: ''
      }
    },
    initData() {
      const query = JSON.parse(JSON.stringify(this.query))
      this.tableLoading = true
      getPage(query).then(result => {
        this.tableLoading = false
        this.query.total = result.data.total
        this.tableData = result.data.list
      }).catch(() => {
        this.tableLoading = false
      })
    },
    flushPage() {
      this.clearQuery()
      this.initData()
    },
    handleQuery() {
      this.query.page = 1
      this.initData()
    },
    handleDetail(row) {
      this.obj = row
      this.dialogType = 'detail'
      this.formDialogVisible = true
    },
    handleUpdate(row) {
      this.obj = row
      this.dialogType = 'update'
      this.formDialogVisible = true
    },
    handleDel(row) {
      this.$confirm('确认删除此数据?', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        del([row.id]).then(result => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.handleQuery()
        }).catch(() => {
          this.$message.error('删除失败')
        })
      }).catch(() => {
        this.$message.info('已取消')
      })
    },
    handleAddUser() {
      this.obj = null
      this.dialogType = 'add'
      this.formDialogVisible = true
    },
    closeDialog() {
      this.formDialogVisible = false
      this.alarmDialogVisible = false
      this.jurisdictionDialogVisible = false
    },
    // 报警配置
    deployAlarm(row) {
      this.obj = row
      this.alarmDialogVisible = true
    },
    // 分配权限
    allotJurisdiction(row) {
      this.obj = row
      this.jurisdictionDialogVisible = true
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
